package com.yuchen.ishop.service;

import com.yuchen.core.cache.J2CacheUtils;
import com.yuchen.ishop.Gobals;
import com.yuchen.ishop.entity.Dictionary;
import com.yuchen.ishop.service.dictionary.AddressRestClient;
import com.yuchen.ishop.service.dictionary.DictionaryRestClient;
import com.yuchen.utils.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class CacheService {

    @Autowired
    private DictionaryRestClient dictionaryRestClient;

    @Autowired
    private AddressRestClient addressRestClient;

    public List<Dictionary> setDictionaryCache() {
        J2CacheUtils.evict(Gobals.CACHE_DICTIONARY, Gobals.CACHE_DICTIONARY_GOODS_TYPE);
        List<Dictionary> goodsTypes = dictionaryRestClient.find("goods_type");
        J2CacheUtils.set(Gobals.CACHE_DICTIONARY, Gobals.CACHE_DICTIONARY_GOODS_TYPE, goodsTypes);
        return goodsTypes;
    }

    public List<Dictionary> getDictionaryCache() {
        List<Dictionary> goodsTypes = (List<Dictionary>) J2CacheUtils.get(Gobals.CACHE_DICTIONARY, Gobals.CACHE_DICTIONARY_GOODS_TYPE);
        if(goodsTypes==null||goodsTypes.size()<=0){
            goodsTypes = setDictionaryCache();
        }
        return goodsTypes;
    }

    public List<Map> setAddressCache() {
        J2CacheUtils.evict(Gobals.CACHE_DICTIONARY, Gobals.CACHE_ADDRESS);
        List<Map> addresses = addressRestClient.find();
        J2CacheUtils.set(Gobals.CACHE_DICTIONARY, Gobals.CACHE_ADDRESS, addresses);
        return addresses;
    }

    public List<Map> getAddressCache() {
        List<Map> addresses = (List<Map>) J2CacheUtils.get(Gobals.CACHE_DICTIONARY, Gobals.CACHE_ADDRESS);
        if(addresses==null||addresses.size()<=0){
            addresses = setAddressCache();
        }
        return addresses;
    }




}
